package fun.yao.common.monitor.info;

import fun.yao.common.Constant;
import fun.yao.common.msg.HashtableMsg;
import fun.yao.common.uitls.LogUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * 队列信息信息
 * @author longjinglai
 *
 */
public class QueueInfo implements IMonitorInfo {

    private List<QueueDetail> alQueueDetail = new ArrayList<QueueDetail>();

    public List<QueueDetail> getQueueDetail()
    {
        return this.alQueueDetail;
    }

    public QueueInfo()
    {


    }

    public QueueInfo(HashtableMsg htm)
    {
        for(int i=0;i<htm.size();i++)
        {
            Hashtable<String, String> mapQueueInfo = htm.get(i);
            if(mapQueueInfo!=null)
            {
                QueueDetail detail = new QueueDetail(mapQueueInfo.get("queue_name"),Integer.parseInt(mapQueueInfo.get("queue_size")) );
                this.alQueueDetail.add(detail);
            }
        }
    }

    /**
     * 转换为HashtableMsg
     * @return
     */
    public HashtableMsg toHashtableMsg()
    {
        HashtableMsg htm = new HashtableMsg();
        for(int i=0;i<this.alQueueDetail.size();i++)
        {
            Hashtable<String, String> mapQueueInfo = new Hashtable<String, String>();
            mapQueueInfo.put("queue_name", this.alQueueDetail.get(i).getQueueName());
            mapQueueInfo.put("queue_size", this.alQueueDetail.get(i).getQueueSize()+"");
            htm.add(mapQueueInfo);
        }
        return htm;
    }



    @Override
    public String toString() {
        return "QueuelInfo [alQueueDetail=" + alQueueDetail + "]";
    }



    public class QueueDetail
    {
        /**
         * 队列名称
         */
        private String queueName = "";

        /**
         * 队列大小
         */
        private int queueSize = 0;


        public QueueDetail(String queueName, int queueSize)
        {
            this.queueName = queueName;
            this.queueSize = queueSize;
        }


        public String getQueueName() {
            return queueName;
        }


        public void setQueueName(String queueName) {
            this.queueName = queueName;
        }

        public int getQueueSize() {
            return queueSize;
        }

        public void setQueueSize(int queueSize) {
            this.queueSize = queueSize;
        }

        @Override
        public String toString() {
            return "QueueDetail [queueName=" + queueName + ", queueSize="
                    + queueSize + "]";
        }

    }



    public void print(StringBuilder sbOut) {
        try
        {
            for(int i=0;i<this.getQueueDetail().size();i++)
            {
                QueueDetail detailInfo = this.getQueueDetail().get(i);
                sbOut.append("第" + i + "个queue["+detailInfo.getQueueSize()+"]使用情况\n");
                sbOut.append("queue名称："+detailInfo.getQueueName()+"，queue大小："+detailInfo.getQueueSize()+"\n");
            }
        }catch (Exception e) {
            LogUtil.WriteLog(Constant.BADLY_ERROR, e);
        }
    }
}
